﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Tetris2020.Scripts.UI
{
    public class TetrisGoalPanel : MonoBehaviour
    {
        [Tooltip("当前游戏的累计得分")] [SerializeField]
        public Text txtGoal;

        [Tooltip("当前游戏的难度等级. 取值范围[0, 15] 之间")] [SerializeField]
        public Text txtLevel;

        [Tooltip("预览方块的面板. 用于显示下一个下落的俄罗斯方块")] [SerializeField]
        public GameObject previewTetrisBoxPanel;

        [Tooltip("俄罗斯方块难度升级对应的需求经验")] [SerializeField]
        public int[] levelUpExpAry;

        /// <summary>
        /// 预览的方块列表
        /// </summary>
        private readonly Queue<GameObject> _previewTetrisBoxQueue = new Queue<GameObject>();

        /// <summary>
        /// 当前预览显示的方块
        /// </summary>
        private GameObject _currentPreviewTetrisBox;

        private int _mScore;

        public int Score
        {
            get { return _mScore; }
            set
            {
                _mScore = value;
                txtGoal.text = "" + _mScore;

                for (var i = 0; i < this.levelUpExpAry.Length; i++)
                {
                    if (_mScore >= this.levelUpExpAry[i])
                    {
                        this.Level = i;
                    }
                }
            }
        }

        private int _mLevel;

        public int Level
        {
            get { return _mLevel; }
            set
            {
                _mLevel = value;
                txtLevel.text = "" + _mLevel;
            }
        }


        /// <summary>
        ///   显示下一个方块
        /// </summary>
        public void ShowNextTetrisBox()
        {
            if (_currentPreviewTetrisBox != null)
            {
                Destroy(_currentPreviewTetrisBox);
            }

            var tetrisBox = this._previewTetrisBoxQueue.Dequeue();
            if (tetrisBox != null)
            {
                tetrisBox.SetActive(true);
                this._currentPreviewTetrisBox = tetrisBox;
            }
        }

        public void Offer(GameObject go)
        {
            this._previewTetrisBoxQueue.Enqueue(go);
        }
    }
}